package com.holly.unit.system.modular.user.controller;

import com.holly.unit.core.pojo.response.ResponseData;
import com.holly.unit.core.pojo.response.SuccessResponseData;
import com.holly.unit.scanner.api.annotation.ApiResource;
import com.holly.unit.scanner.api.annotation.GetResource;
import com.holly.unit.security.api.DragCaptchaApi;
import com.holly.unit.security.api.ImageCaptchaApi;
import com.holly.unit.security.api.pojo.DragCaptchaImageDTO;
import com.holly.unit.security.api.pojo.ImageCaptcha;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

import static com.holly.unit.core.constants.RuleConstants.BASE64_IMG_PREFIX;

/**
 * 图形验证码
 *
 * @author holly
 * @date 2021/1/15 15:11
 */
@RestController
@ApiResource(name = "用户登录图形验证码")
@Api(tags = "用户登录图形验证码")
public class KaptchaController {

    @Resource
    private ImageCaptchaApi captchaApi;

    @Resource
    private DragCaptchaApi dragCaptchaApi;

    /**
     * 获取图形验证码
     *
     * @author holly
     * @date 2021/7/5 12:00
     */
    @ApiOperation(value = "获取图形验证码" ,notes = "获取图形验证码")
    @GetResource(name = "获取图形验证码", path = "/captcha", requiredPermission = false, requiredLogin = false, responseClass = ImageCaptcha.class)
    public ResponseData captcha() {
        return new SuccessResponseData(captchaApi.captcha());
    }

    /**
     * 获取拖拽验证码
     *
     * @author holly
     * @date 2021/7/5 12:00
     */
    @ApiOperation(value = "获取图形验证码" ,notes = "获取图形验证码")
    @GetResource(name = "获取图形验证码", path = "/dragCaptcha", requiredPermission = false, requiredLogin = false, responseClass = DragCaptchaImageDTO.class)
    public ResponseData dragCaptcha() {
        DragCaptchaImageDTO captcha = dragCaptchaApi.createCaptcha();
        captcha.setSrcImage(BASE64_IMG_PREFIX + captcha.getSrcImage());
        captcha.setCutImage(BASE64_IMG_PREFIX + captcha.getCutImage());
        return new SuccessResponseData(captcha);
    }

}
